﻿//======================================================================
//
//	Cross-Platform XNA Menu System
//  http://cpxms.codeplex.com/
//
//  All code and content licensed under Microsoft Public License (Ms-PL)	
//
//======================================================================
//
//	File:		PagePicker.cs
//
//	Author:		MJP
//
//	Date:		04/17/09
//
//======================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;
using System.Reflection;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Drawing.Design;

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;


using CPXMenuItem = CPXMenu.MenuItem;
using WFMenuItem = System.Windows.Forms.MenuItem;

namespace CPXMenu
{
	/// <summary>
	/// PropertyGrid drop-down control that lets you pick
	/// a Page from a MenuProject.  You can actually tell the 
	/// PropertyGrid to display a list of possible values for a Type
	/// but...this was easier. :P
	/// </summary>
    internal partial class PagePicker : UserControl
    {
        MenuProject project;

		/// <summary>
		/// Gets the Page selected by the user
		/// </summary>
        public Page SelectedPage
        {
            get { return project.MenuPages[listBox.SelectedIndex]; }
        }

		/// <summary>
		/// Creates a new PagePicker instance that's bound to a project
		/// </summary>
		/// <param name="project">The MenuProject in which the PagePicker will look for pages</param>
        public PagePicker(MenuProject project)
        {
            InitializeComponent();

            this.project = project;

            for (int i = 0; i < project.Children.Count; i++)
            {
                listBox.Items.Add(project.MenuPages[i]);
                if (project.MenuPages[i] == project.CurrentPage)
                    listBox.SelectedIndex = i;
            }
        }
    }

	/// <summary>
	/// UITypeEditor for Page properties that uses PagePicker to let the user select pages
	/// </summary>
    public class PagePickerEditor : UITypeEditor
    {
		/// <summary>
		/// Gets the style that will be used for editing. Always returns DropDown.
		/// </summary>
		/// <param name="context">The editing context</param>
		/// <returns>The edit style to use</returns>
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

		/// <summary>
		/// Allows the user to edit a value by popping up PagePicker
		/// </summary>
		/// <param name="context">The editing context</param>
		/// <param name="provider">The service provider</param>
		/// <param name="value">The current value</param>
		/// <returns>The value selected by the user</returns>
        public override object EditValue(ITypeDescriptorContext context,
                                            IServiceProvider provider,
                                            object value)
        {
            IWindowsFormsEditorService editorService = null;

            if (provider != null)
            {
                editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            }

            if (editorService != null)
            {
                MenuItem item = (MenuItem)context.Instance;
                PagePicker pagePicker = new PagePicker(item.GetParentProject());

				// Go go gadget PagePicker!
                editorService.DropDownControl(pagePicker);

                value = pagePicker.SelectedPage;
            }

            return value;
        }
    }

}
